package com.javacv.plus.platform;

import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Arrays;
import java.util.List;

/**
 * 平台实体类
 */
@Data
@EqualsAndHashCode
public class Platform {

    /**
     * 操作系统名称
     */
    private String os;

    /**
     * 架构
     */
    private String arch;

    /**
     * 完整平台字符串
     */
    private String platformString;

    /**
     * 支持的操作系统列表
     */
    public static final List<String> SUPPORTED_OS = Arrays.asList(
            "windows", "linux", "macosx", "android", "ios"
    );

    /**
     * 支持的架构列表
     */
    public static final List<String> SUPPORTED_ARCH = Arrays.asList(
            "x86_64", "x86", "arm64", "arm"
    );

    private Platform(String os, String arch) {
        this.os = os;
        this.arch = arch;
        this.platformString = os + "-" + arch;
    }

    /**
     * 创建平台实例
     */
    public static Platform of(String os, String arch) {
        validateOS(os);
        validateArch(arch);
        return new Platform(os, arch);
    }

    /**
     * 从字符串解析平台
     */
    public static Platform parse(String platformString) {
        if (platformString == null || platformString.trim().isEmpty()) {
            throw new IllegalArgumentException("平台字符串不能为空");
        }

        String[] parts = platformString.split("-");
        if (parts.length != 2) {
            throw new IllegalArgumentException("无效的平台字符串格式，应为: os-arch");
        }

        return Platform.of(parts[0], parts[1]);
    }

    /**
     * 校验操作系统
     */
    private static void validateOS(String os) {
        if (!SUPPORTED_OS.contains(os)) {
            throw new IllegalArgumentException("不支持的操作系统: " + os + 
                    "，支持的系统: " + SUPPORTED_OS);
        }
    }

    /**
     * 校验架构
     */
    private static void validateArch(String arch) {
        if (!SUPPORTED_ARCH.contains(arch)) {
            throw new IllegalArgumentException("不支持的架构: " + arch + 
                    "，支持的架构: " + SUPPORTED_ARCH);
        }
    }

    /**
     * 检查是否为Windows平台
     */
    public boolean isWindows() {
        return "windows".equals(os);
    }

    /**
     * 检查是否为Linux平台
     */
    public boolean isLinux() {
        return "linux".equals(os);
    }

    /**
     * 检查是否为macOS平台
     */
    public boolean isMacOS() {
        return "macosx".equals(os);
    }

    /**
     * 检查是否为Android平台
     */
    public boolean isAndroid() {
        return "android".equals(os);
    }

    /**
     * 检查是否为iOS平台
     */
    public boolean isIOS() {
        return "ios".equals(os);
    }

    /**
     * 检查是否为x86_64架构
     */
    public boolean isX86_64() {
        return "x86_64".equals(arch);
    }

    /**
     * 检查是否为ARM架构
     */
    public boolean isARM() {
        return "arm".equals(arch) || "arm64".equals(arch);
    }

    /**
     * 检查是否为桌面平台
     */
    public boolean isDesktop() {
        return isWindows() || isLinux() || isMacOS();
    }

    /**
     * 检查是否为移动平台
     */
    public boolean isMobile() {
        return isAndroid() || isIOS();
    }

    @Override
    public String toString() {
        return platformString;
    }

    /**
     * 常用平台常量
     */
    public static final Platform WINDOWS_X86_64 = Platform.of("windows", "x86_64");
    public static final Platform LINUX_X86_64 = Platform.of("linux", "x86_64");
    public static final Platform MACOSX_X86_64 = Platform.of("macosx", "x86_64");
    public static final Platform MACOSX_ARM64 = Platform.of("macosx", "arm64");
    public static final Platform ANDROID_ARM = Platform.of("android", "arm");
    public static final Platform ANDROID_ARM64 = Platform.of("android", "arm64");
} 